/* 
 * Bitmain Tech, Home of Professional Bitmain Tech
 * Copyright 2014 Bitmain Tech Inc. and/or its affiliates and other contributors
 * as indicated by the @author tags. All rights reserved.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 */
package com.bitmain.mmst.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bitmain.mmst.core.base.BasicController;
import com.bitmain.mmst.core.struct.APIResult;
import com.bitmain.mmst.core.struct.PagedResult;
import com.bitmain.mmst.core.struct.ServiceResult;
import com.bitmain.mmst.domain.MinerType;
import com.bitmain.mmst.web.service.MinerTypeService;

/**
 * 矿机类型控制器
 *
 * @author Kweny - enkun.jiang@bitmain.com
 * @since 2015年12月29日
 *
 */
@Controller
@RequestMapping(value = "miner")
public class MinerTypeController extends BasicController {
	
	@Autowired
	private MinerTypeService minerTypeService;

	// 页面：矿机类型
	@RequestMapping(value = "type", method = RequestMethod.GET)
	public ModelAndView type() {
		ModelAndView mv = new ModelAndView("miner/type");
		return mv;
	}
	
	// 接口：查询
	@RequestMapping(value = "types", method = RequestMethod.GET)
	@ResponseBody
	public PagedResult<MinerType> get(HttpServletRequest request) {
		PagedResult<MinerType> result = new PagedResult<MinerType>();
		String name = request.getParameter("search");
		String sortName = request.getParameter("sort");
		String sortOrder = request.getParameter("order");
		Map<String, Object> params = new HashMap<String, Object>();
		if (StringUtils.isNoneBlank(name)) {
			params.put("name", name);
		}
		if (StringUtils.isNotBlank(sortName)) {
			params.put("sortName", sortName);
		}
		if (StringUtils.isNotBlank(sortOrder)) {
			params.put("sortOrder", sortOrder);
		}
		ServiceResult<List<MinerType>> rs = minerTypeService.getAll(params);
		if (BooleanUtils.isTrue(rs.getSuccess())) { // 成功
			result.setSuccess(true);
			result.setTotal(rs.getData().size());
			result.setRows(rs.getData());
		} else { // 错误
			result.setError(request, rs.getCode());
		}
		return result;
	}
	/*
	// 接口：添加
	@RequestMapping(value = "types", method = RequestMethod.POST)
	@ResponseBody
	public APIResult post(HttpServletRequest request, @RequestBody MinerType type) {
		APIResult result = new APIResult();
		
		result = verify(request, result, type);
		if (BooleanUtils.isNotTrue(result.getSuccess())) {
			return result;
		}
		
		// 设置达标算力不小于0且不大于理论算力
		if (type.getHashPass() < 0) {
			type.setHashPass(0);
			type.setHashPassRate(0);
		} else if (type.getHashPass() > type.getHashTheo()) {
			type.setHashPass(type.getHashTheo());
			type.setHashPassRate(100);
		}
		ServiceResult<?> rs = minerTypeService.addMinerType(type);
		if (BooleanUtils.isTrue(rs.getSuccess())) { // 成功
			Object[] args = new Object[] {1, getMessage(request, "domain.minertype") };
			result.setInfo(request, INFO.SUCCESS_ADD, args);
			result.setData(rs.getData());
		} else {
			if (rs.getCode() == ERROR.VER_DUPLICATE) { // 矿机类型名称重复
				Object[] args = new Object[] { getMessage(request, "domain.minertypename"), type.getName() };
				result.setError(request, ERROR.VER_DUPLICATE, args);
			} else { // 其它错误
				result.setError(request, rs.getCode());
			}
		}
		
		return result;
	}
	*/
	// 接口：修改
	@RequestMapping(value = "types", method = RequestMethod.PUT)
	@ResponseBody
	public APIResult put(HttpServletRequest request, @RequestBody MinerType type) {
		APIResult result = new APIResult();
		
		result = verify(request, result, type);
		if (BooleanUtils.isNotTrue(result.getSuccess())) {
			return result;
		}
		
		// 设置达标算力不小于0且不大于理论算力
		if (type.getHashPass() < 0) {
			type.setHashPass(0);
			type.setHashPassRate(0);
		} else if (type.getHashPass() > type.getHashTheo()) {
			type.setHashPass(type.getHashTheo());
			type.setHashPassRate(100);
		}
		ServiceResult<?> rs = minerTypeService.modifyMinerType(type);
		if (BooleanUtils.isTrue(rs.getSuccess())) { // 成功
			Object[] args = new Object[] {1, getMessage(request, "domain.minertype") };
			result.setInfo(request, INFO.SUCCESS_MODIFY, args);
			result.setData(rs.getData());
		} else {
			if (rs.getCode() == ERROR.VER_DUPLICATE) { // 名称重复
				Object[] args = new Object[] { getMessage(request, "domain.minertypename"), type.getName() };
				result.setError(request, ERROR.VER_DUPLICATE, args);
			} else if (rs.getCode() == ERROR.VER_OCCUPIED) { // 其它用户正在操作该数据
				Object[] args = new Object[] { getMessage(request, "domain.minertype") };
				result.setError(request, ERROR.VER_OCCUPIED, args);
			} else if (rs.getCode() == ERROR.VER_NONEXISTENT) { // 数据不存在或被删除
				Object[] args = new Object[] { getMessage(request, "domain.minertype") };
				result.setError(request, ERROR.VER_NONEXISTENT, args);
			} else { // 其它错误
				result.setError(request, rs.getCode());
			}
		}
		
		return result;
	}
	/*
	// 接口：删除
	@RequestMapping(value = "types", method = RequestMethod.DELETE)
	@ResponseBody
	public APIResult delete(HttpServletRequest request, @RequestBody List<MinerType> types) {
		APIResult result = new APIResult();
		if(CollectionUtils.isEmpty(types)) {
			Object[] args = new Object[] { getMessage(request, "domain.minertype") };
			result.setError(request, ERROR.VER_EMPTY_COLLECTION, args);
			return result;
		}
		ServiceResult<?> rs = minerTypeService.removeMinerTypes(types);
		if (BooleanUtils.isTrue(rs.getSuccess())) { // 成功
			Object[] args = new Object[] {types.size(), getMessage(request, "domain.minertype") };
			result.setInfo(request, INFO.SUCCESS_REMOVE, args);
			result.setData(rs.getData());
		} else { // 错误
			result.setError(request, rs.getCode());
		}
		return result;
	}
	*/
	private APIResult verify(HttpServletRequest request, APIResult result, MinerType type) {
		if (type == null) { // 接收的数据为null
			Object[] args = new Object[] { getMessage(request, "domain.minertype") };
			result.setError(request, ERROR.VER_NULL, args);
			return result;
		}
		/*if (StringUtils.isBlank(type.getName())) { // 名称为空
			Object[] args = new Object[] { getMessage(request, "domain.minertypename") };
			result.setError(request, ERROR.VER_BLANK, args);
			return result;
		}
		if (type.getName().length() < 2 || type.getName().length() > 10) { // 名称少于2个字符或多于10个字符
			Object[] args = new Object[] { getMessage(request, "domain.minertypename"), 2, 10 };
			result.setError(request, ERROR.VER_LENGTH, args);
			return result;
		}*/
		if (type.getPort() <= 0 || type.getPort() > 65535) { // 矿机端口无效
			Object[] args = new Object[] { getMessage(request, "domain.minerport") };
			result.setError(request, ERROR.VER_INVALID, args);
			return result;
		}
		if (type.getPower() <= 0) { // 矿机功耗无效
			Object[] args = new Object[] { getMessage(request, "domain.minertypepower") };
			result.setError(request, ERROR.VER_INVALID, args);
			return result;
		}
		if (type.getHashTheo() < 0 || type.getHashTheo() > 10000) { // 无效的理论算力
			Object[] args = new Object[] { getMessage(request, "domain.hashtheo") };
			result.setError(request, ERROR.VER_INVALID, args);
			return result;
		}
		if (type.getHashPassRate() < 0 || type.getHashPassRate() > 100) { // 无效的算力达标比率
			Object[] args = new Object[] { getMessage(request, "domain.hashpassrate") };
			result.setError(request, ERROR.VER_INVALID, args);
			return result;
		}
		if (type.getFanNum() < 0 || type.getFanNum() > 100) { // 无效的风扇数
			Object[] args = new Object[] { getMessage(request, "domain.fannum") };
			result.setError(request, ERROR.VER_INVALID, args);
			return result;
		}
		if (type.getFanMaxSpeed() < 0 || type.getFanMaxSpeed() > 10000) { // 无效的风扇最大速度
			Object[] args = new Object[] { getMessage(request, "domain.fanmaxspeed") };
			result.setError(request, ERROR.VER_INVALID, args);
			return result;
		}
		if (type.getTempNum() < 0 || type.getTempNum() > 100) { // 无效的温感数
			Object[] args = new Object[] { getMessage(request, "domain.tempnum") };
			result.setError(request, ERROR.VER_INVALID, args);
			return result;
		}
		if (type.getTempMaxValue() < 0 || type.getTempMaxValue() > 100) { // 无效的最高温度
			Object[] args = new Object[] { getMessage(request, "domain.tempmaxvalue") };
			result.setError(request, ERROR.VER_INVALID, args);
			return result;
		}
		if (type.getChainMinNum() < 0 || type.getChainMinNum() > 100) { // 无效的最少链条数
			Object[] args = new Object[] { getMessage(request, "domain.chainminnum") };
			result.setError(request, ERROR.VER_INVALID, args);
			return result;
		}
		if (type.getChipMinNum() < 0 || type.getChipMinNum() > 1000) { // 无效的最少芯片数
			Object[] args = new Object[] { getMessage(request, "domain.chipminnum") };
			result.setError(request, ERROR.VER_INVALID, args);
			return result;
		}
		result.setInfo(request, INFO.INFO);
		return result;
	}
}
